/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-5-17 上午10:23
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.recycleviewdemo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.dqhl.recycleviewdemo.R;
import com.example.dqhl.recycleviewdemo.modle.StraggedModle;

import java.util.List;

/**
 * Created by dqhl on 17/5/17.
 */
public class StraggedGridAdapter extends RecyclerView.Adapter<StraggedGridAdapter.MyViewHolder> {
    private List<StraggedModle> mModleList;
    private Context context;
    private onItemClickListener onItemClickListener;

    public StraggedGridAdapter(Context context, List<StraggedModle> myModleList) {
        this.context = context;
        this.mModleList = myModleList;
    }

    //点击事件和长按事件
    public interface onItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }


    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder myViewHolder = new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_stagged, parent, false));

        return myViewHolder;
    }

//    public void addData(int position){
//        MyModle myModle = new MyModle();
//        myModle.setId("猴子你怪骚来");
//        mModleList.add(position,myModle);
//        notifyItemInserted(position);
//    }
    public void removeData(int position){
        mModleList.remove(position);
        notifyItemRemoved(position);

    }
    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.tv_text.setText(mModleList.get(position).getText());
        holder.iv_pic.setImageResource(mModleList.get(position).getImg());

        //如果设置了回调，则设置点击事件
        if(onItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    int position = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView,position);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    int position = holder.getLayoutPosition();
                    onItemClickListener.onItemLongClick(holder.itemView,position);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mModleList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_text;
        ImageView iv_pic;

        public MyViewHolder(View view) {
            super(view);
            tv_text = (TextView) view.findViewById(R.id.tv_text);
            iv_pic = (ImageView) view.findViewById(R.id.iv_pic);
        }
    }
}
